package com.kzw.oa.system.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.oa.system.model.UserOrg;

@Service
@Transactional
public class UserOrgService extends DefaultEntityManager<UserOrg, Long> {

	/**
	 * 取某一用户所有的部门
	 */
	@Transactional(readOnly = true)
	public List<UserOrg> getDepOrgsByUserId(Long userId) {
		String hql = "from UserOrg uo where uo.appUser.userId=?";
		return findByHQL(hql, userId);
	}

	public List<UserOrg> getByUserIdOrgId(Long userId, Long orgId) {
		String hql = "from UserOrg uo where uo.appUser.userId=? and uo.organization.orgId=?";
		return findByHQL(hql, userId, orgId);
	}
	
	/**
	 * 删除UserOrg对象
	 */
	public void delCascade(Long userId, Long orgId){
		String hql = "delete UserOrg uo where uo.appUser.userId = "+userId;
		if(orgId != 0){
			hql += " and uo.organization.orgId="+ orgId;
		}
		getEntityDao().batchExecute(hql);
	}
}
